package de.koller.worlders.engine.tiles.data;

import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.util.ColorIntUtil;
import de.koller.worlders.WorldersRoot;
import de.koller.worlders.engine.entity.zone.ErrorZone;
import de.koller.worlders.engine.tiles.landscape.ImageGenerator;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class MapData {
	
	public static final int MAP_SIZE = 128;
	
	private final int tx, ty;
	private final String key;
	
	private final String name;

	private BufferedImage lightMap;
	private BufferedImage map;
	private BufferedImage drawMap;
	
	private MapDetails details;
	
	private WorldersRoot preRoot;
	private int loadedTiles = 0;
	
	public MapData( int tx, int ty, String key, String name, WorldersRoot preRoot ) {
		this.tx = tx; 
		this.ty = ty;
		this.key = key;
		
		this.preRoot = preRoot;
		this.name = name;
	
		//@todo nicht in bildern sondern arrays speichern
		try {	
			map = ImageIO.read( new File( getPrePath() + name + key + "_map.png" ) );
			lightMap = ImageIO.read( new File( getPrePath() + name + key + "_lightMap.png" ) );
		} catch( Exception ex ) {
			int ms = MAP_SIZE;
			map = new BufferedImage( ms, ms, BufferedImage.TYPE_INT_ARGB );
			lightMap = new BufferedImage( ms, ms, BufferedImage.TYPE_INT_ARGB );
			new ImageGenerator( this.preRoot ).generate( tx, ty, map, lightMap );
		}
		
		try {	
			drawMap = ImageIO.read( new File( getPrePath() + name + key + "_draw.png" ) );
		} catch( Exception ex ) {
			int ms = MAP_SIZE;
			drawMap = new BufferedImage( ms, ms, BufferedImage.TYPE_INT_ARGB );
		}
		
		details = new MapDetails( this );
	}
	
	public String getPrePath() {
		return preRoot.getUser().getUserpath();
	}
	
	int getDrawData( int x, int y ) {
		if( !inBounds( x, y ) )
			return ColorIntUtil.getIntColor( 0xff, 0, 0, 0 );
		return drawMap.getRGB( x - tx, y - ty );
	}
	
	int getMapData( int x, int y ) {
		if( !inBounds( x, y ) )
			return ColorIntUtil.getIntColor( ErrorZone.GEN_ID, 0, 0, 0 );
		return map.getRGB( x - tx, y - ty );
	}
	
	int getLightData( int x, int y ) {
		if( !inBounds( x, y ) )
			return ColorIntUtil.getIntColor( 0xff, 0, 0, 0 );
		return lightMap.getRGB( x - tx, y - ty );
	}
	
	void setMapData( int x, int y, int data ) {
		if( !inBounds( x, y ) )
			return;
		map.setRGB( x - tx, y - ty, data );
	}
	
	void setLightData( int x, int y, int data ) {
		if( !inBounds( x, y ) )
			return;
		lightMap.setRGB( x - tx, y - ty, data );
	}
	
	void setDrawData( int x, int y, int data ) {
		if( !inBounds( x, y ) )
			return;
		drawMap.setRGB( x - tx, y - ty, data );
	}
	
	private boolean inBounds( int x, int y ) {
		if( x < tx )
			return false;
		if( y < ty )
			return false;
		if( x >= tx+MAP_SIZE )
			return false;
		if( y >= ty+MAP_SIZE )
			return false;
		return true;
	}
	
	public void save() {
		try {
			ImageIO.write( map, "png", new File( preRoot.getUser().getUserpath() + name + key + "_map.png" ) );
			ImageIO.write( lightMap, "png", new File( preRoot.getUser().getUserpath() + name + key + "_lightMap.png" ) );
			ImageIO.write( drawMap, "png", new File( preRoot.getUser().getUserpath() + name + key + "_draw.png" ) );
			details.save();
			System.out.println( "Maps saved: " + name );
		} catch( Exception ex ) {
			throw new OceanException( ex );
		}
	}

	public WorldersRoot getPreRoot() {
		return preRoot;
	}

	public String getName() {
		return name;
	}

	public MapDetails getDetails() {
		return details;
	}
	
	synchronized public void tileLoaded() {
		loadedTiles++;
	}
	
	synchronized public void tileReleased() {
		loadedTiles--;
		
		if( loadedTiles == 0 )
			preRoot.getTileMap().tileHolder.tileLoader.mapDataDist.releaseMap( this );
	}
	
	public int loadedTileNumber() {
		return loadedTiles;
	}

	public String getKey() {
		return key;
	}
	
}
